<template>
  <el-container class="home_container">
    <!-- 主页头部 -->
    <el-header>
      <div>
        <img src="@/assets/home_logo.jpg" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 下面部分 -->
    <el-container>
      <!-- 侧边菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath">
          <!-- 菜单折叠按钮 -->
          <div class="zhedie" @click="zhedie">
            <span v-if="isCollapse">三</span>
            <span v-else>| | |</span>
          </div>
          <!-- 一级菜单 -->
          <el-submenu :index="item.order + ''" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <!-- 一级图标 -->
              <i :class="menuIcon[item.id]"></i>
              <!-- 一级标题 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + mitem.path"
              v-for="mitem in item.children"
              :key="mitem.id"
              @click="keepActivePath('/' + mitem.path)">
              <!-- 二级图标 -->
              <i class="el-icon-menu"></i>
              <!-- 二级标题 -->
              <span>{{ mitem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data() {
    return {
      // 菜单列表
      menusList: [],
      // 菜单图标
      menuIcon: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-lock_fill',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 是否折叠
      isCollapse: false,
      // 激活菜单路由
      activePath: '',
    }
  },
  created() {
    // 进入页面就请求侧边菜单数据
    this.getMenusList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录功能
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 请求菜单列表数据
    async getMenusList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return res.meta.msg
      this.menusList = res.data
    },
    // 菜单折叠功能
    zhedie() {
      this.isCollapse = !this.isCollapse
    },
    // 保存当前激活菜单路径
    keepActivePath(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    },
  },
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    img {
      width: 52px;
      height: 52px;
      align-items: center;
    }
    span {
      font-size: 20px;
      margin-left: 15px;
      color: white;
    }
  }
}
.el-aside {
  background-color: #24282a;
  .el-menu {
    // width: 100%;
    border-right: none;
    .el-submenu i {
      margin-right: 5px;
    }
    .zhedie {
      color: white;
      display: flex;
      justify-content: space-around;
      background-color: #222528;
    }
  }
}
.el-main {
  flex: 1;
  background-color: aliceblue;
}
</style>
